package com.ruyuan.seckilling.aggr.controller;

import com.ruyuan.dependency.product.api.ProductApi;
import com.ruyuan.dependency.product.vo.SkuVo;
import com.ruyuan.dependency.product.vo.SpuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zhonghuashishan
 */
@RestController
public class SeckillingProductAggrController {

    @Autowired
    private ProductApi productApi;

    // 参数示例
    // .params(ImmutableMap.of("skuId", skuId))
    @PostMapping("/seckilling/product")
    public Map aggr(@RequestBody Map params) {
        Long skuId = Long.parseLong(String.valueOf(params.get("skuId")));

        SkuVo skuVo = productApi.queryBySkuId(skuId);
        SpuVo spuVo = productApi.queryBySpuId(skuVo.getSpuId());

        Map aggrData = new LinkedHashMap();
        aggrData.put("brandId", spuVo.getBrandId());
        aggrData.put("brandName", spuVo.getBrandName());
        aggrData.put("brandLogo", spuVo.getBrandLogo());
        aggrData.put("categoryId", spuVo.getCategoryId());
        aggrData.put("categoryName", spuVo.getCategoryName());

        aggrData.put("skuId", skuVo.getId());
        aggrData.put("skuName", skuVo.getName());
        aggrData.put("price", skuVo.getPrice());
        aggrData.put("seckillingPrice", skuVo.getSeckillingPrice());
        aggrData.put("image", skuVo.getImage());
        String[] images = skuVo.getImages().split(",");
        for (int i = 0; i < images.length; i++) {
            String image = images[i];
            aggrData.put("image" + i, image);
        }
        return aggrData;
    }
}
